package com.jrtc.service.impl;

import com.jrtc.base.entity.bo.Doctor;
import com.jrtc.base.entity.bo.Menu;
import com.jrtc.base.entity.bo.Role;
import com.jrtc.base.entity.bo.RoleMenu;
import com.jrtc.dao.MenuDao;
import com.jrtc.dao.RoleDao;
import com.jrtc.dao.RoleMenuDao;
import com.jrtc.service.MenuService;
import com.jrtc.service.RoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.minio.ObjectReadArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.jrtc.common.MenuCommon.getChildMenu;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2022-09-05 18:46:03
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private RoleMenuDao roleMenuDao;
    @Resource
    private MenuDao menuDao;



    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer id) {
        Role role = this.roleDao.queryById(id);
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(id);
        List<Menu> menuList = menuDao.getMenuList();
        List<RoleMenu> lstRoleMenu = roleMenuDao.queryAll(roleMenu);
        List<Menu> lstTmpMenu = new ArrayList<>();
        for (Menu menu: menuList) {
            for (RoleMenu obj: lstRoleMenu) {
                if (menu.getId().equals(obj.getMenuId())){
                    menu.setAdd(obj.getAdd());
                    menu.setDelete(obj.getDelete());
                    menu.setQuery(obj.getQuery());
                    menu.setEdit(obj.getEdit());
                    menu.setAudit(obj.getAudit());
                    menu.setCheckMenu("1");
                }
            }
        }
       /* for (RoleMenu obj: lstRoleMenu)
        {
            Menu m  = menuDao.queryById(obj.getMenuId());
            m.setAdd(obj.getAdd());
            m.setDelete(obj.getDelete());
            m.setQuery(obj.getQuery());
            m.setEdit(obj.getEdit());
            m.setAudit(obj.getAudit());
            lstTmpMenu.add(m);
        }*/
        List<Menu> lstMenu = menuList;
        List<Menu> lstData = new ArrayList<>();
        for (Menu rootMenu : lstMenu)
        {
            if (rootMenu.getMenuId() == null)
            {
                getChildMenu(lstMenu, rootMenu);
                lstData.add(rootMenu);
            }
        }
        role.getLstMenus().addAll(lstData);
        return role;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Role> queryAllByLimit(int offset, int limit) {
        return this.roleDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<Role> queryRoleList(Role role)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Role> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(role.getPageNo(), role.getPageSize());
        IPage<Role> rolePage = this.roleDao.queryRoleList(page, role);
        return rolePage;
    }
    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(Role role) {
        this.roleDao.insert(role);
        for (Menu m : role.getLstMenus())
        {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setAdd(m.getAdd());
            roleMenu.setDelete(m.getDelete());
            roleMenu.setQuery(m.getQuery());
            roleMenu.setEdit(m.getEdit());
            roleMenu.setAudit(m.getAudit());
            roleMenu.setMenuId(m.getMenuId());
            this.roleMenuDao.insert(roleMenu);
        }
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        this.roleDao.update(role);
        this.roleMenuDao.deleteByRoleId(role.getId());
        for (Menu m : role.getLstMenus())
        {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setAdd(m.getAdd());
            roleMenu.setDelete(m.getDelete());
            roleMenu.setQuery(m.getQuery());
            roleMenu.setEdit(m.getEdit());
            roleMenu.setAudit(m.getAudit());
            roleMenu.setMenuId(m.getMenuId());
            this.roleMenuDao.insert(roleMenu);
        }
        return this.queryById(role.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.roleDao.deleteById(id) > 0;
    }
    /**
     * 获取角色列表
     */
    @Override
    public List<Role> dirRoleList() {
        List<Role> roles = roleDao.dirRoleList();
        return roles;
    }

}
